/**
* WP Showroom Admin JS
*/

window.addEvent('domready', function(){

	$$('.WPSR_tr').each(function(el, i) {
		el.addEvent('mouseenter', function() {
			el.addClass('WPSR_tr_hover');
		});
		el.addEvent('mouseleave', function() {
			el.removeClass('WPSR_tr_hover');
		});
	});
	
	$$('.WPSR_td_sel').each(function(el, i) {
		el.addEvent('click', function() {
			hid = $(el).getParent().get('id');
			WPSR_toggleChecked('chb'+hid);
		});
	});
	
});

function WPSR_toggleChecked(id) {
	if ($(id).checked == true) {
		$(id).checked = false;
	}
	else {
		$(id).checked = true;
	}
}

function WPSR_check(id) {
	$(id).checked = true;
}

function WPSR_toggle_open(id) {
	$(id).toggleClass('WPSR_close');
}

function WPSR_toggle_current(id) {
	$(id).toggleClass('WPSR_current');
}

function WPSR_goto(url) {
	window.location.href=url;
}

function WPSR_ajax(id,param) {
	$(id).addClass('WPSR_loading');
	var req = new Request.HTML({
		url:'../wp-content/plugins/WPshowroom/ajax.php?'+param, 
		onSuccess: function(html) {
			$(id).removeClass('WPSR_loading');
			$(id).set('text', '');
			$(id).adopt(html);
		},
		onFailure: function() {
			$(id).removeClass('WPSR_loading');
			$(id).set('text', 'The request failed.');
		}
	}).send();
}

function WPSR_insertpics(path) {
	
	var tr = new Element('tr').inject('inserthere', 'top');
	var td = new Element('td', {styles: {'height': '20px'}}).inject(tr, 'top');
	
	WPSR_ajax(td,'event=insert&path='+path);	
}

function WPSR_regenratepics() {
	
	var tr = new Element('tr').inject('inserthere', 'top');
	var td = new Element('td', {styles: {'height': '20px'}}).inject(tr, 'top');
	
	WPSR_ajax(td,'event=regenerate');	
}

function WPSR_edit_alb(id,button) {
	$('wpsr-edit-res-'+id).set('text', '');
	ajax = false;
	if ($('wpsr-edit-'+id).hasClass('WPSR_close')) {
		ajax = true;
	}
	WPSR_toggle_open('wpsr-edit-'+id);
	WPSR_toggle_current(button);
	if (ajax) {
		WPSR_ajax('wpsr-edit-res-'+id,'event=editalb&id='+id);
	}	
}

function WPSR_edit_pic(id,button) {
	$('wpsr-edit-res-'+id).set('text', '');
	$('wpsr-'+id).toggleClass('WPSR_nbb');
	ajax = false;
	if ($('wpsr-edit-'+id).hasClass('WPSR_close')) {
		ajax = true;
	}
	WPSR_toggle_open('wpsr-edit-'+id);
	WPSR_toggle_current(button);
	if (ajax) {
		WPSR_ajax('wpsr-edit-res-'+id,'event=editpic&id='+id);
	}
}

function WPSR_load_div(id,par) {
	$(id).set('text', '');
	WPSR_ajax(id,'event=loaddiv&id='+par);
}

function WPSR_load_thumb(file,where) {
	$('thumbcont').set('text', '');
	WPSR_ajax('thumbcont','event=loadthumb&file='+file+'&where='+where);
}

function WPSR_loadwomsg_div(id,par) {
	if ($('message')) {
		$('message').setStyle('display', 'none');
	}
	WPSR_load_div(id,'event=loaddiv&id='+par);
}

function WPSR_apoptions(opt) {
	$('wpsr-alblist').set('text', '');
	if (opt == "assign") {
		WPSR_load_div('wpsr-alblist',2);
	}
	else if (opt == "delete") {
		WPSR_load_div('wpsr-alblist',3);
	}
	else if (opt == "unassign") {
		WPSR_load_div('wpsr-alblist',4)
	}
}

function WPSR_peoptions(aid, button) {
	$('wpsr-edit-res-all').set('text', '');
	ajax = false;
	if ($('wpsr-edit-all').hasClass('WPSR_close')) {
		ajax = true;
	}
	WPSR_toggle_open('wpsr-edit-all');
	WPSR_toggle_current(button);
	if (ajax) {
		WPSR_load_div('wpsr-edit-res-all','5&aid='+aid);
	}	
}

function WPSR_send(id, log) {
	
	//Set the options of the form's Request handler. 
	//("this" refers to the $('myForm') element).
	$(id).set('send', {
		onComplete: function(response) { 
			//log.removeClass('ajax-loading');
			$(log).set('html', response);
		}});
	//Send the form.
	$(id).send();
}